package com.module.product.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.util.StringUtil;

import com.module.product.vo.Category;
/**
*
* 商品分类dao类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品分类]
*/
public class CategoryDao extends GenericHibernateDao<Category, String>{

	private static final CategoryDao instance = new CategoryDao();
	
	private CategoryDao(){}
	
	public synchronized static CategoryDao getInstance(){
		return instance;
	}
	   /**
     * <逻辑删除>
     * <功能详细描述>
     * @param attrGroup
     * @return
     * @see [类、类#方法、类#成员]
     */
    public boolean deleteObjs(Session session,Category obj){
        boolean flag = false;
        try {
            String hql = "update Category set updaterId = ?,updateTime=?,status =? where id in ("+obj.getId()+")";
            Query query  = session.createQuery(hql); 
                  query.setParameter(0,obj.getUpdaterId());
                  query.setParameter(1, new Date());
                  query.setParameter(2, CUDEnum.D.toString());
                  query.executeUpdate();
            flag = true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return flag;
    }
    
    
    @SuppressWarnings("unchecked")
	public List<Category> getCategoryTree(Session session,String name,String opid){
        List<Category> list = null ;
        List<Category> childrenList = null;
        StringBuffer sb = new StringBuffer("from Category as obj where obj.status <>? and pid=?");
        if(!StringUtil.isNull(name))
        	sb.append(" and obj.name like '%"+name+"%'");
//        if(!StringUtil.isNull(orgId))
//    		sb.append(" and obj.orgId = '"+orgId+"'");
        sb.append("order by createTime desc");
        Query query = session.createQuery(sb.toString());
              query.setParameter(0,CUDEnum.D.toString());
              query.setParameter(1,opid);
              list = query.list();  
            for(Category c :list){
                  childrenList = getCategoryTree(session,name,c.getId());
                  c.setChildren(childrenList.size()==0?null:childrenList);
            }    
     return list;
    }
    /**
     * 
     * @param session
     * @param orgId
     * @param pid
     * @return
     */
    @SuppressWarnings("unchecked")
	public List<Category> getOrgCategoryTree(Session session,String orgId,String pid){
    	  List<Category> list = null ;
          List<Category> childrenList = null;
          StringBuffer sb = new StringBuffer("from Category as obj where obj.status <>? and pid=?");
//          if(!StringUtil.isNull(orgId))
//          	sb.append(" and obj.orgId = '"+orgId+"'");    
          sb.append("order by createTime desc");
          Query query = session.createQuery(sb.toString());
                query.setParameter(0,CUDEnum.D.toString());
                query.setParameter(1,orgId);
                list = query.list();  
              for(Category c :list){
                    childrenList = getOrgCategoryTree(session,orgId,c.getId());
                    c.setChildren(childrenList.size()==0?null:childrenList);
              }    
       return list;
    }
    
    /**
     * 查询产品分类是否被引用
     * @param session
     * @param cid
     * @return
     */
    public long getProductByCategory(Session session,String cid){
    	String sql = "select count(*) from p_product where c_id in " +
    			"(select product_id from p_category_product where category_id ="+cid+") and c_status <> ?";
    	Query query  = session.createSQLQuery(sql);
    	query.setParameter(0, CUDEnum.D.toString()) ;
    	return (long)((java.math.BigInteger) query.uniqueResult()).intValue();    	
    }
    
    public String getCategoryIdByName(Session session,String name){
    	String sql = "from Category as obj where obj.status <>? and name = '" +name+"'";
    	Query query  = session.createQuery(sql);
    	query.setParameter(0, CUDEnum.D.toString()) ;
    	List<Category> list = query.list();
    	if(!list.isEmpty() && list.size()>0){
    		return list.get(0).getId();
    	}
    	return null;
    }
}
